package logic

import (
	"context"
	"fmt"
	"goodsrpc/goods"
	"goodsrpc/internal/svc"
	"goodsrpc/mysql"
	"goodsrpc/pkg"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameLogic {
	return &RealNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RealNameLogic) RealName(in *goods.RealNameRequest) (*goods.RealNameResponse, error) {
	// todo: add your logic here and delete this line
	var r mysql.RealName
	err := r.Real(int(in.UserId))
	if err != nil {
		logx.Info("查询失败")
		return nil, err
	}
	if r.ID != 0 {
		logx.Info("用户已实名")
		return nil, fmt.Errorf("用户已实名")
	}
	if !pkg.RealName(in.RealName, in.CardNo) {
		logx.Info("实名认证失败")
		return nil, fmt.Errorf("实名认证失败")
	}
	name := mysql.RealName{
		CardNo:   in.CardNo,
		RealName: in.RealName,
	}
	mysql.DB.Debug().Create(&name)

	return &goods.RealNameResponse{}, nil
}
